<template>
  <div class="search-container">
    <!-- 搜索组件一级路由   $router.back()返回上一个页面-->
    <van-nav-bar
      left-arrow
      title="搜索中心"
      @click-left="$router.push({ path: '/home' })"
    >
    </van-nav-bar>

    <!-- https://youzan.github.io/vant/#/zh-CN/search -->
    <van-search
      placeholder="请输入搜索关键词"
      shape="round"
      v-model.trim="keyword"
      @input="inputHandler"
      @search="toResult"
    >
      <template #action>
        <div>搜索</div>
      </template>
    </van-search>

    <!-- 联想建议 -->
    <van-cell-group>
      <van-cell
        v-for="(item, index) in newOptions"
        :key="index"
        :title="item"
        icon="search"
        @click="toResultWithQuery(index)"
      >
        <template #title>
          <div v-html="item" class="box"></div>
        </template>
      </van-cell>
    </van-cell-group>
    <!-- /联想建议 -->
  </div>
</template>

<script>
import { getSearchSuggestion } from '../../api/search'
export default {
  name: 'search',
  data () {
    return {
      keyword: '', // 搜索关键字
      options: [],
      timeoutId: undefined,
      canSearch: true
    }
  },
  computed: {
    newOptions () {
      const ops = this.options.map(v => {
        // console.log(v)
        return v.replace(
          this.keyword,
          `<span style='color:red'>${this.keyword}</span>`
        )
      })
      return ops
    }
  },
  methods: {
    toResultWithQuery (index) {
      this.$router.push({ path: '/result', query: { q: this.options[index] } })
    },
    toResult () {
      this.$router.push({ path: '/result', query: { q: this.keyword } })
    },
    // 节流 立即执行，等超过了规定事件，才能再次触发
    async inputHandler () {
      if (this.keyword === '') {
        this.options = []
        return
      }

      if (this.canSearch === true) {
        this.canSearch = false
        const data = await getSearchSuggestion({ q: this.keyword })
        this.options = data.data.data.options

        this.timeoutId = setTimeout(async () => {
          this.canSearch = true
        }, 500)
      }
    }

    // 防抖  不会立即执行，先等等看，在一定时间内没有再次接触，才执行
    // inputHandler () {
    //   clearTimeout(this.timeoutId)

    //   this.timeoutId = setTimeout(async () => {
    //     if (this.keyword === '') {
    //       this.options = []
    //       return
    //     }
    //     // console.log(this.keyword)
    //     const data = await getSearchSuggestion({ q: this.keyword })
    //     // console.log(data)
    //     this.options = data.data.data.options
    //   }, 500)

    //   // 基础版
    //   // if (this.keyword === '') {
    //   //   this.options = []
    //   //   return
    //   // }
    //   // // console.log(this.keyword)
    //   // const data = await getSearchSuggestion({ q: this.keyword })
    //   // // console.log(data)
    //   // this.options = data.data.data.options
    // }
  }
}
</script>

<style lang="less">
.search-container {
  .van-nav-bar {
    background-color: #3296fa;
    .van-nav-bar__title {
      color: white;
    }
    .van-icon {
      color: white;
    }
  }
}
.van-cell {
  .van-cell__title {
    .box {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      width: 300px;
    }
  }
}
</style>
